<?php

declare(strict_types=1);

namespace App\Listener\Validator;

use Hyperf\Event\Annotation\Listener;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Validation\Event\ValidatorFactoryResolved;
use Psr\Container\ContainerInterface;
use Hyperf\Event\Contract\ListenerInterface;
use function Hyperf\Support\make;
use function Hyperf\Config\config;

#[Listener]
class ValidatorFactoryResolvedListener implements ListenerInterface
{
    public function __construct()
    {
    }

    public function listen(): array
    {
        return [
            ValidatorFactoryResolved::class
        ];
    }

    public function process(object $event): void
    {
        /** @var ValidatorFactoryInterface $validatorFactory */
        $validatorFactory = $event->validatorFactory;

        // 手机号
        $validatorFactory->extend('mobile', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^1[3456789]\d{9}$/', (string)$value));
        });
        // 当创建一个自定义验证规则时，你可能有时候需要为错误信息定义自定义占位符这里扩展了 :mobile 占位符
        $validatorFactory->replacer('mobile', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':mobile', $attribute, $message);
        });
        // ICON图片
        $validatorFactory->extend('icon', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^[\- \da-zA-Z]+$/', $value));
        });
        $validatorFactory->replacer('icon', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':icon', $attribute, $message);
        });
        // 路径
        $validatorFactory->extend('path', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^[ \/_?&=\-.:\da-zA-Z]+$/', $value));
        });
        $validatorFactory->replacer('path', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':path', $attribute, $message);
        });
        // 应用
        $validatorFactory->extend('app', function ($attribute, $value, $parameters, $validator){
            return in_array($value, config('app'));
        });
        $validatorFactory->replacer('app', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':app', $attribute, $message);
        });
        // 邮政编码
        $validatorFactory->extend('zip_code', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^\d{6}$/', (string)$value));
        });
        $validatorFactory->replacer('zip_code', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':zip_code', $attribute, $message);
        });
        // 营业执照号/社会统一信用码
        $validatorFactory->extend('business_no', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^([159Y])([1239])([0-9ABCDEFGHJKLMNPQRTUWXY]{6})([0-9ABCDEFGHJKLMNPQRTUWXY]{9})([0-90-9ABCDEFGHJKLMNPQRTUWXY])$/', $value));
        });
        $validatorFactory->replacer('business_no', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':business_no', $attribute, $message);
        });
        // 银行卡号码
        $validatorFactory->extend('bank_no', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^[1-9](\d{14}|\d{18})$/', $value));
        });
        $validatorFactory->replacer('bank_no', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':bank_no', $attribute, $message);
        });
        // 税号
        $validatorFactory->extend('duty_paragraph', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^[A-Z0-9]{15}$|^[A-Z0-9]{17}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/', $value));
        });
        $validatorFactory->replacer('duty_paragraph', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':duty_paragraph', $attribute, $message);
        });
        // 组织机构代码
        $validatorFactory->extend('orgnization_code', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^[a-zA-Z0-9]{10,20}$/', $value));
        });
        $validatorFactory->replacer('orgnization_code', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':orgnization_code', $attribute, $message);
        });
        // 纳税人识别号
        $validatorFactory->extend('tax_no', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/', $value));
        });
        $validatorFactory->replacer('tax_no', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':tax_no', $attribute, $message);
        });
        // 身份证号码
        $validatorFactory->extend('id_card', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/', $value));
        });
        $validatorFactory->replacer('id_card', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':id_card', $attribute, $message);
        });
        // 验证唯一标识
        $validatorFactory->extend('id', function ($attribute, $value, $parameters, $validator){
            return make($parameters[0])->where('id', $value)->exists();
        });
        $validatorFactory->replacer('id', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':id', $attribute, $message);
        });
        // 验证唯一标识
        $validatorFactory->extend('ids', function ($attribute, $value, $parameters, $validator){
            return make($parameters[0])->whereIn('id', $value)->count() === count($value);
        });
        $validatorFactory->replacer('ids', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':ids', $attribute, $message);
        });
        // 验证账号
        $validatorFactory->extend('account', function ($attribute, $value, $parameters, $validator){
            return preg_match('/^1[3456789]\d{9}$/', $value) || preg_match('/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/', $value) || preg_match('/^\d+$/', $value);
        });
        $validatorFactory->replacer('account', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':account', $attribute, $message);
        });
        // 验证时间格式
        $validatorFactory->extend('time', function ($attribute, $value, $parameters, $validator){
            return boolval(strtotime($value));
        });
        $validatorFactory->replacer('time', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':time', $attribute, $message);
        });
        // 验证域名格式
        $validatorFactory->extend('domain', function ($attribute, $value, $parameters, $validator){
            return boolval(preg_match('/^([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,}$/', (string)$value));
        });
        $validatorFactory->replacer('domain', function ($message, $attribute, $rule, $parameters) {
            return str_replace(':domain', $attribute, $message);
        });
    }
}
